<?php
class User extends CI_Controller {
	
   
	function __construct()
	{
		parent::__construct();
		$this->load->model('ModelLogin');
		$this->controller = 'user';
		
	}
	
  
	function index()
	{
		
		$this->functions->checkAdmin($this->controller.'/');
		$config['base_url'] = base_url($this->controller."/index/");
	    
		
		
		/*$data['count_member'] = $this->ModelLogin->get_count(MEMBERMASTER,'registration_date','last');
		$data['count_Order'] = $this->ModelLogin->get_count(ORDERMASTER,'order_date','last');
		$data['count_news'] = $this->ModelLogin->get_count(NEWSLETTERUSER,'posted_date','last');
		$data['cur_count_member'] = $this->ModelLogin->get_count(MEMBERMASTER,'registration_date');
		$data['cur_count_Order'] = $this->ModelLogin->get_count(ORDERMASTER,'order_date');
		$data['cur_count_news'] = $this->ModelLogin->get_count(NEWSLETTERUSER,'posted_date');
		$data['recordset'] = $this->ModelLogin->get_dashboard();
		$data['count'] = count($data['recordset']);
		$data['view_more'] =base_url('orders/');
		$data['view_link']        	= front_base_url()."order/invoice/{{ID}}/1/";
		$data['user_role']             = $this->nsession->userdata('user_session_role');*/
		$data['succmsg'] = $this->nsession->userdata('succmsg');
		$data['errmsg'] = $this->nsession->userdata('errmsg');
		
		$this->nsession->set_userdata('succmsg', "");
		$this->nsession->set_userdata('errmsg', "");
		
		
		
		$elements = array();
		$elements['menu'] = 'menu/index';
		$elements['main'] = 'user/home';

		$element_data['menu'] = array();
		$element_data['main'] = $data;

		$this->layout->setLayout('layout_main_view'); 
		$this->layout->multiple_view($elements,$element_data);
	}
	
   /**
	* Function changeemail().
	* used For Change Email
	* @author Biplab Biswas
	* @date 
	*/
		
	function changeemail()
	{
		$this->functions->checkAdmin($this->controller.'/changeemail/');
		
		$id = $this->nsession->userdata('user_session_id');		
		$email = $this->ModelLogin->getEmail($id);		
		$data['email'] = $email;
		
		$data['succmsg'] = $this->nsession->userdata('succmsg');
		$data['errmsg'] = $this->nsession->userdata('errmsg');
		
		$this->nsession->set_userdata('succmsg', "");
		$this->nsession->set_userdata('errmsg', "");
		
		$elements = array();
		$elements['menu'] = 'menu/index';;
		$elements['main'] = 'user/changeemail';

		$element_data['menu'] = array();
		$element_data['main'] = $data;
		
		$this->layout->setLayout('layout_main_view'); 
		$this->layout->multiple_view($elements,$element_data);
	}
	
	/**
	* Function do_changeemail().
	* used for save changed email 
	* @author Biplab Biswas
	* @date
	*/
	
	function do_changeemail()
	{
		$this->functions->checkAdmin($this->controller.'/changeemail/');
		
		$this->form_validation->set_rules('new_email', 'Email', 'trim|required|valid_email|xss_clean');
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		$id = $this->nsession->userdata('user_session_id');
		
		if($this->form_validation->run() == TRUE)
		{
			$data['email_address'] = $this->input->request('new_email','');
			$this->ModelLogin->updateAdminEmail($id,$data);
			$this->nsession->set_userdata('succmsg',"Email Updated");
			redirect(base_url($this->controller."/changeemail/"));
			return true;
		}else{
			$this->nsession->set_userdata('errmsg',"Email Not Updated");
			redirect(base_url($this->controller."/changeemail/"));
			return true;
		}
	}
	
	
	
	function changepass()
	{
		$this->functions->checkAdmin($this->controller.'/changepass/');
		$data['msg'] = "";
		$id = $this->nsession->userdata('user_session_id');
		
		$data['succmsg'] = $this->nsession->userdata('succmsg');
		$data['errmsg'] = $this->nsession->userdata('errmsg');
		
		$this->nsession->set_userdata('succmsg', "");
		$this->nsession->set_userdata('errmsg', "");
		
		$elements = array();
		$elements['menu'] = 'menu/index';
		$elements['main'] = 'user/changepass';

		$element_data['menu'] = array();
		$element_data['main'] = $data;
		
		$this->layout->setLayout('layout_main_view'); 
		$this->layout->multiple_view($elements,$element_data);
	}
	
	/**
	 * Function do_changepass().
	 * used for save change password.
	 * @author Biplab Biswas.
	 * @date 
	 */
	
	function do_changepass()
	{
	    $this->functions->checkAdmin($this->controller.'/changepass/');
	
		$this->form_validation->set_rules('old_admin_pwd', 'Old Password', 'trim|required|min_length[5]|max_length[20]|xss_clean');
		$this->form_validation->set_rules('new_admin_pwd', 'New Password', 'trim|required|min_length[5]|max_length[20]|matches[conf_new_admin_pwd]|xss_clean');
		$this->form_validation->set_rules('conf_new_admin_pwd', 'Comfirm New Password','trim|required|min_length[5]|max_length[20]|xss_clean');
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
	
		$id = $this->nsession->userdata('user_session_id');
		$data['msg'] = "";
	
		if($this->form_validation->run() == TRUE)
		{
			$data['oldpassword'] = $this->input->request('old_admin_pwd');
			$isTrue = $this->ModelLogin->valideOldPassword($data);
	
			if($isTrue){
				$data['new_admin_pwd'] = $this->input->request('new_admin_pwd');
				$count = $this->ModelLogin->updateAdminPass($id,$data['new_admin_pwd']);
				
				if($count > 0){
					
				$rs = $this->ModelLogin->getProfileData($id);
				$toemail = $rs[0]['email_address'];
				$this->emailPasswordchange($toemail);
					
				}
				$this->nsession->set_userdata('succmsg',"Password Updated");
			}else{
				$this->nsession->set_userdata('errmsg',"Invalid Old Password");
			}
	        redirect(base_url($this->controller."/changepass/"));
			return true;
		}else{
			$this->changepass();
			return true;
		}
	}
	
	
	function changeprofile()
	{
		$this->functions->checkAdmin($this->controller.'/changeprofile/');
		
		$id = $this->nsession->userdata('user_session_id');
		$data['msg'] = "";
		$rs = $this->ModelLogin->getProfileData($id);
					
		if(is_array($rs)){
			foreach($rs[0] as $key =>$val)
			{
				$data[$key] = $val;
			}
		}
		$data['succmsg'] = $this->nsession->userdata('succmsg');
		$data['errmsg'] = $this->nsession->userdata('errmsg');
		
		$this->nsession->set_userdata('succmsg', "");
		$this->nsession->set_userdata('errmsg', "");
		
		$elements = array();
		$elements['menu'] = 'menu/index';
		$elements['main'] = 'user/changeprofile';

		$element_data['menu'] = array();
		$element_data['main'] = $data;
		
		$this->layout->setLayout('layout_main_view'); 
		$this->layout->multiple_view($elements,$element_data);
	}
	
	
	
	function do_changeprofile()
	{
		$this->functions->checkAdmin($this->controller.'/changeprofile/');
		
		$this->form_validation->set_rules('full_name', 'Name', 'trim|required|xss_clean');
		$this->form_validation->set_rules('email_address', 'Email','trim|required|valid_email|xss_clean');		
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		
		$id = $this->nsession->userdata('user_session_id');
		$data['msg'] = "";
		
		$rs = $this->ModelLogin->getProfileData($id);
						
		if(is_array($rs)){
			foreach($rs[0] as $key =>$val){
				$data[$key] = $val;
			}
		}
		
		if($this->form_validation->run() == TRUE){
			$data['full_name']	= $this->input->request('full_name','');
			$data['email_address'] = $this->input->request('email_address','');
			
			$before_rs = $this->ModelLogin->getProfileData($id);
			$count = $this->ModelLogin->profileUpdate($id,$data);
			$rs = $this->ModelLogin->getProfileData($id);
			
			if($count > 0){
				$toemail = $rs[0]['email_address'];
				if($before_rs[0]['email_address']!= $toemail){
					$cc = $before_rs[0]['email_address'];
					
				}else{
					$cc = '';
				}
				
				$this->emailChangeprofile($toemail,$cc);
				
				
			}
			if(is_array($rs)){
				foreach($rs[0] as $key =>$val)
				{
					$data[$key] = $val;
				}
			}
			$this->nsession->set_userdata('succmsg',"Profile Updated Successfully");
			redirect(base_url($this->controller."/changeprofile/"));
			return true;
		 }else{
			$this->changeprofile(); 
			return true;
		 }
	}
	
	function emailPasswordchange($toemail)
	{
	   	$to_email		= $toemail;
		$template_code	=	"ADMIN_EMAIL_PASSWORD_CHANGE";
		//$search_text	=	array("[PASSWORD]");
		//$replace_text	=	array($data['password'],);
		$search_text	=	'';
		$replace_text	=	'';
		$subject		=	"";
		$name			=	GLOBAL_SITE_NAME;
		$email			=	GLOBAL_WEBADMIN_EMAIL;
		
		if($this->functions->send_email($to_email,$template_code,$search_text,$replace_text,$subject,$name,$email)){
			return true;
		}else{
			return false;
		}
	}
	
	function emailChangeprofile($toemail,$cc)
	{
	   	$to_email		= $toemail;
		$template_code	=	"ADMIN_EMAIL_CHANGE_PROFILE";
		
		$search_text	=	'';
		$replace_text	=	'';
		$subject		=	"";
		$name			=	GLOBAL_SITE_NAME;
		$email			=	GLOBAL_WEBADMIN_EMAIL;
		
		if($this->functions->send_email($to_email,$template_code,$search_text,$replace_text,$subject,$name,$email,$cc)){
			return true;
		}else{
			return false;
		}
	}
	
}
?>